package tec.line;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class PanelDomainProvider {
	
	Executor executor = Executors.newFixedThreadPool(5);
	
	private int real_width = 20;//实体宽度
	private int black_width = 12;//空白宽度
	private int px_width = 800;//panel所占横向像素值
	private int px_hight = 600;//panel所占纵向像素值
	private int left_fix = 5;
	
	public PanelDomain createPanelDomain(){
		
		PanelDomain pd = new PanelDomain();
		
		pd.setReal_width(real_width);
		pd.setBlack_width(black_width);
		pd.setPx_width(px_width);
		pd.setPx_hight(px_hight);
		pd.setLeft_fix(left_fix);
		setPara(pd);
		calculate(pd);
		
		return pd;
	}

	private void calculate(final PanelDomain pd) {
		final CountDownLatch latch = new CountDownLatch(1);
		final CountDownLatch latch_end = new CountDownLatch(1);
		
		Runnable run = new Runnable() {
			
			public void run() {
				int index_max_real = 0;//指数最大值
				int index_min_real = 0;//指数最小值
				
				List<IndexData> list = pd.getDatas();
				for (int i = 0; i < list.size(); i++) {
					if (index_max_real < list.get(i).getIndex_max()) 
						index_max_real = list.get(i).getIndex_max();
					if (index_min_real > list.get(i).getIndex_min() || index_min_real == 0)
						index_min_real = list.get(i).getIndex_min();
				}
				
				pd.setIndex_max_real(index_max_real);
				pd.setIndex_min_real(index_min_real);
				pd.setIndex_max_show((50 - (index_max_real%50)) + index_max_real);
				pd.setIndex_min_show(index_min_real - (50 - (index_min_real%50)));
				pd.setIndex_domain(pd.getIndex_max_show() - pd.getIndex_min_show());
				latch.countDown();
			}
		};
		
		executor.execute(run);
		
		Runnable run_datas = new Runnable() {
			
			List<IndexData> list = pd.getDatas();
			public void run() {
				try {
					latch.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				for (int i = 0; i < list.size(); i++) {
					IndexData idata = list.get(i);
					idata.getP_max().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()) + pd.getReal_width()/2 -1);
					idata.getP_max().setY((int)(((float)idata.getIndex_max() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
					
					idata.getP_min().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()) + pd.getReal_width()/2 -1);
					idata.getP_min().setY((int)(((float)idata.getIndex_min() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
					
					idata.getP_open().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()));;
					idata.getP_open().setY((int)(((float)idata.getIndex_open() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
					idata.getP_close().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()));
					idata.getP_close().setY((int)(((float)idata.getIndex_close() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
				}
				
				latch_end.countDown();
			}
		};
		executor.execute(run_datas);
		
		try {
			latch.await();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void setPara(PanelDomain pd) {
		XMLpara xml = new XMLpara();
		List<IndexData> list = null;
		try {
			list = xml.para();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//        Random random = new Random();
//        int start = random.nextInt(30);
//        int length = pd.getPx_width()/(pd.getBlack_width() + pd.getReal_width());
//        
//        List<IndexData> dataList = new ArrayList<IndexData>();
//        
//        dataList.addAll(list.subList(start-1, start + length -1));
		pd.setDatas(list);
	}
	
	
	

}
